import pygame
import gui
from gui import Widget,Container
from pygo1963.view.constants import FONDO_PATH, NEGRA_PATH, BLANCA_PATH
from pygo1963.view.gui import Label
from pygo1963.view import defaultStyle
from pygo1963.model.Constants import BLACK_COLOR


class GoBoard(Widget,Container):
    '''
    Go Board 
    '''
    WINDOWS_SIZE = (480,480)
    BLACK = (0,0,0)
    
    def __init__(self, game, delta = (0,0), position = (0,0), size = (120,20), parent = None, style = None, enabled = True):
        '''
        GoBoardTest Constructor
        '''
        
        self.turn = ""
        
        if (game.next_move == BLACK_COLOR):
            self.turn ="Black "
        else:
            self.turn ="White"
        
        #TODO funcion que cree los star points en funcion al size     
        self.star_points = []
        self.game = game
        self._board_size = game.board.size
        self.create_surfaces()    
        self.delta = delta
        
        defaultStyle.init(gui)
        self.gui = gui
        self.game_over_style = self.gui.gameOverStyle.copy()
        self.turn_style = self.gui.turn_style.copy()
        self.windows = None
        Widget.__init__(self,position,size,parent,style,enabled)    
        Container.__init__(self)
        self.turn_label = None
        self.black_pieces = []
        self.white_pieces = []
    
    def update_pieces(self,board):
        self.black_pieces = board.get_pieces()[0]
        self.white_pieces = board.get_pieces()[1]
                
    def draw_star_points(self, dx, dy, empty_size_x, empty_size_y):
        '''
        Draw handicap points
        '''
        for x, y in self.star_points:
            
            xPos, yPos = int(empty_size_x + ( x - 1 ) *  dx), int(empty_size_y + (y - 1) * dy);
            pygame.draw.circle(self.fondo, self.BLACK, (xPos,yPos), 7, 0)            
            
    def create_surfaces(self):
        #self.screen = pygame.display.set_mode(self.WINDOWS_SIZE)
        self.screen = pygame.Surface((self.WINDOWS_SIZE[0],self.WINDOWS_SIZE[1]))
        self.create_board_surface()
        self.create_piece_surfaces()
    
    def create_board_surface(self):
        self.fondo = pygame.image.load(FONDO_PATH)
        
        size = self.game.board.size
        width = self.WINDOWS_SIZE[0]
        height = self.WINDOWS_SIZE[0]
        
        dx = width / float(size)
        dy = height / float(size)
        
        self.dx = dx
        self.dy = dy
        
        empty_size_x = dx / 2.0
        empty_size_y = dy / 2.0
        
        self.empty_size_x = empty_size_x
        self.empty_size_y = empty_size_y
        
        #dibujar las lineas en el tablero 
        for number_of_line in xrange(size):
            
            pygame.draw.line(self.fondo, self.BLACK, (empty_size_x + dx * number_of_line, empty_size_y), 
                             (empty_size_x + dx * number_of_line, height - empty_size_y), 2)
            
            pygame.draw.line(self.fondo, self.BLACK, (empty_size_x, dy * number_of_line + empty_size_y),
                             (width - empty_size_x, dy * number_of_line + empty_size_y), 2)
        
        self.draw_star_points(dx, dy, empty_size_x, empty_size_y)
        
    def create_piece_surfaces(self):
        
        #Se crea la piedra negra
        texture=pygame.image.load(NEGRA_PATH).convert()
        texture_rect=texture.get_rect()
        
        mask=pygame.Surface((texture_rect.width,texture_rect.height)) 
        mask.fill((255,255,255))
        pygame.draw.circle(mask,(0,0,0),(texture_rect.width/2,texture_rect.height/2),int(texture_rect.width*0.45))
        
        tmp_image=texture.copy() 
        mask.set_colorkey((0,0,0)) 
        tmp_image.blit(mask,(0,0)) 
        
        tmp_rect=tmp_image.get_rect()
        tmp_rect.center=(400,300)
        tmp_image.set_colorkey((255,255,255))
        
        self.black_piece = tmp_image
        
        self.black_piece = pygame.transform.scale(self.black_piece, (int(self.dx / 1.3),int(self.dx / 1.3)))
        
        #Se crea la piedra blanca
        texture=pygame.image.load(BLANCA_PATH).convert()
        texture_rect=texture.get_rect()
        
        mask=pygame.Surface((texture_rect.width,texture_rect.height)) 
        mask.fill((255,255,255))
        pygame.draw.circle(mask,(0,0,0),(texture_rect.width/2,texture_rect.height/2),int(texture_rect.width*0.45))
        
        tmp_image=texture.copy() 
        mask.set_colorkey((0,0,0)) 
        tmp_image.blit(mask,(0,0)) 
        
        tmp_rect=tmp_image.get_rect()
        tmp_rect.center=(400,300)
        tmp_image.set_colorkey((255,255,255))
        
        self.white_piece = tmp_image
        
        self.white_piece = pygame.transform.scale(self.white_piece, (int(self.dx / 1.3),int(self.dx / 1.3)))
        
    def draw(self, surf):
        
        #ugly
        if self._board_size != self.game.board.size:
            self._board_size = self.game.board.size
            self.create_surfaces()             
        
        self.create_complete_surface(self.black_pieces,self.white_pieces)
        
        screen_rect = self.screen.get_rect()
        screen_rect.center = (screen_rect.center[0] + self.delta[0],screen_rect.center[1] + self.delta[1])
        screen_rect.height = screen_rect.height + 50 
        
        if (self.game.board.game_finished):            
            Label(position = (int(self.WINDOWS_SIZE[0]/4),int(self.WINDOWS_SIZE[1] / 2)),text = "Game Over",style = self.game_over_style,parent = self.windows)
        elif (self.game.next_move == BLACK_COLOR):
            self.turn ="Black "            
        else:
            self.turn ="White"
        
        if (self.turn_label == None):
            self.turn_label = Label(text = "",style = self.turn_style,parent = self.windows,position = (150,13))
        
        self.turn_label.text = "Next Turn: " + self.turn
            
        surf.blit(self.screen,screen_rect)
        
        
        
    def create_complete_surface(self,black_pieces,white_pieces):
            
        self.screen.fill(self.BLACK)
        self.screen.blit(self.fondo,self.fondo.get_rect())
        pygame.display.set_caption("pygo1963")
        self.draw_pieces()
    
    
    def draw_pieces(self):
        white_pieces, black_pieces = self.game.board.get_pieces()
         
        for black_piece in black_pieces:
            curr_rect = self.black_piece.get_rect()
            
            xPos = self.empty_size_x + (black_piece[0])* self.dx
            yPos = self.empty_size_y + (self.game.board.size -1 - black_piece[1]) * self.dy 
            
            curr_rect.center=(xPos,yPos)
            self.screen.blit(self.black_piece,curr_rect)
        
        for white_piece in white_pieces:
            curr_rect = self.white_piece.get_rect()
            
            xPos = self.empty_size_x + (white_piece[0])* self.dx
            yPos = self.empty_size_y + (self.game.board.size -1 - white_piece[1]) * self.dy 
            
            curr_rect.center=(xPos,yPos)
            self.screen.blit(self.white_piece,curr_rect)
        
            